const state = {
  token: uni.getStorageSync('token'),
  userInfo: uni.getStorageSync('userInfo') || {},
  roles: [],
  permissions: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    uni.setStorageSync('token', token)
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
    uni.setStorageSync('userInfo', userInfo)
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  }
}

const actions = {
  // 设置token
  setToken({ commit }, token) {
    commit('SET_TOKEN', token)
  },
  // 设置用户信息
  setUserInfo({ commit }, userInfo) {
    commit('SET_USER_INFO', userInfo)
  },
  // 设置角色
  setRoles({ commit }, roles) {
    commit('SET_ROLES', roles)
  },
  // 设置权限
  setPermissions({ commit }, permissions) {
    commit('SET_PERMISSIONS', permissions)
  }
}

const getters = {
  token: state => state.token,
  userInfo: state => state.userInfo,
  roles: state => state.roles,
  permissions: state => state.permissions,
  isLogin: state => !!state.token
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 